#include<bits/stdc++.h>
using namespace std;
const int MOD = 1e9+7;
const int N = 3e4+10;
const int K = 10;
int f[1<<K];
int g[1<<K];

int main(){
    int c,t;
    cin>>c>>t;
    while(t--){
        int n,m,k;
        cin>>n>>m>>k;
        for(int i = 2;i<=n;i++){
            int f;
            cin>>f;
        }
        if(m == 0){
            cout<<1<<'\n';
			continue;
        }
        if(m == 1){
            cout<<n - 1 + n<<'\n';
			continue;
        }
        if(m == 2){
            int res = (1ll*n*n%MOD + 1ll*((n - 1)*n*2ll)%MOD)%MOD;
            res = (res + (n - 1)*2ll%MOD)%MOD;
            if(k != 0){
                res = (res + (n - 1)*2ll%MOD)%MOD;
            }else{
                res = (res + (n - 1))%MOD;
            }
            res = (res + 1ll*(n - 1)*(n - 2)%MOD)%MOD;
            res = (res + 2ll*n%MOD)%MOD;
            cout<<res<<'\n';
			continue;
        }
        if(n == 1){
            if(k == 0){
                int res = 1;
                for(int i = 1;i<=m;i++){
                    res = 1ll*res*(i + i - 1)%MOD;
                }
                cout<<res<<'\n';
				continue;
			}
        }
		for(int i = 1;i<=n;i++){

		}
    }
}